# 指定 CMake 的最低版本要求
cmake_minimum_required(VERSION 3.5.1)

# 项目信息
project(acl_execute_sqrt)

# 编译选项，使用 C++11 标准
add_compile_options(-std=c++11)

# 设置可执行文件的输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "./")

# ============================ 环境路径配置 ============================

# 获取 DDK（开发包）路径，如果环境变量未定义则设置默认路径
if(NOT DEFINED ENV{DDK_PATH})
    set(INC_PATH "/usr/local/Ascend/ascend-toolkit/latest")
    message(STATUS "Using default DDK path: ${INC_PATH}")
else()
    set(INC_PATH $ENV{DDK_PATH})
    message(STATUS "Using DDK path from environment: ${INC_PATH}")
endif()

# 自定义算子包路径
set(CUST_PKG_PATH "${INC_PATH}/opp/vendors/customize/op_api")

# 获取 NPU 编译用的 stub 库路径
if(NOT DEFINED ENV{NPU_HOST_LIB})
    set(LIB_PATH "/usr/local/Ascend/ascend-toolkit/latest/acllib/lib64/stub")
    set(LIB_PATH1 "/usr/local/Ascend/ascend-toolkit/latest/atc/lib64/stub")
    message(STATUS "Using default stub lib paths.")
else()
    set(LIB_PATH $ENV{NPU_HOST_LIB})
    set(LIB_PATH1 "")  # 如果环境变量已定义，这里手动清空默认路径
    message(STATUS "Using lib path from environment: ${LIB_PATH}")
endif()

# ============================ 头文件与库配置 ============================

# 头文件搜索路径
include_directories(
    ${INC_PATH}/runtime/include
    ${INC_PATH}/atc/include
    ${CUST_PKG_PATH}/include
)

# 动态库搜索路径
link_directories(
    ${LIB_PATH}
    ${LIB_PATH1}
    ${CUST_PKG_PATH}/lib
)

# ============================ 构建目标与链接 ============================

# 添加可执行文件
add_executable(execute_sqrt_op
    main.cpp
)

# 链接所需的动态库
target_link_libraries(execute_sqrt_op
    ascendcl           # Ascend 运行时库
    cust_opapi         # 自定义算子 API 接口
    acl_op_compiler    # ATC 编译器库
    nnopbase           # 自定义算子基础库
    stdc++             # 标准 C++ 库（可选，部分系统默认链接）
)

# 安装规则：将目标程序安装到指定路径
install(TARGETS execute_sqrt_op DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
